Pdf for UWP
ページとオーバーレイの作成
C1PDFの使い方 > ページとオーバーレイの作成

前の例では、コンテンツをドキュメントに追加する操作を C1PdfDocument オブジェクトの作成のすぐ後で開始しました。これは、C1PdfDocument の作成時に空のページがドキュメントに自動的に追加されて、任意の種類のコンテンツを受け入れる準備ができているために可能でした。

最初のページを既に塗りつぶしてある場合は、C1PdfDocumentBase.NewPageメソッドを使用して新しいページを追加できます。

デフォルトでは、ドキュメント内のすべてのページが同じサイズと同じ方向を持ちます。これらのパラメータは、C1PdfDocument コンストラクタで指定できます。ページのサイズと方向も、C1PdfDocument.PaperKindC1PdfDocument.PageSize、および C1PdfDocument.Landscape プロパティを設定することでいつでも変更できます。たとえば、次のコードは、すべての用紙サイズを PaperKind 列挙によって定義してドキュメントを作成します。

Visual Basic
コードのコピー
Dim rect As New Rect(72, 72, 100, 50)
' 一定のフォントとStringFormatオブジェクトを作成します。
Dim font As New Font("Tahoma", 18)
Dim sf As New StringFormat()
sf.Alignment = HorizontalAlignment.Center
sf.LineAlignment = VerticalAlignment.Center
' 各用紙サイズにつき1つのページを作成します。
Dim firstPage As Boolean = True
For Each fi As var In GetType(PaperKind).GetFields(System.Reflection.BindingFlags.[Static] Or System.Reflection.BindingFlags.[Public])
    ' Silverlight/PhoneはEnum.GetValuesを持っていません。
    Dim pk As PaperKind = DirectCast(fi.GetValue(Nothing), PaperKind)
    ' カスタムサイズはスキップします。
    If pk = PaperKind.[Custom] Then
        Continue For
    End If
    ' 最初のページ以降のすべてのページに新しいページを追加します。
    If Not firstPage Then
        pdf.NewPage()
    End If
    firstPage = False
    ' 用紙の種類および方向を設定します。
    pdf.PaperKind = pk
    pdf.Landscape = Not pdf.Landscape
    ' コンテンツをページに描画します。
    Dim text As String = String.Format("PaperKind: [{0}];" & vbCr & vbLf & "Landscape: [{1}];" & vbCr & vbLf & "Font: [Tahoma 18pt]", pdf.PaperKind, pdf.Landscape)
    pdf.DrawString(text, font, Colors.Black, rect, sf)
    pdf.DrawRectangle(Colors.Black, rect)
Next

C#
コードのコピー
Rect rect = new Rect(72, 72, 100, 50);
// 一定のフォントとStringFormatオブジェクトを作成します。
Font font = new Font("Tahoma", 18);
StringFormat sf = new StringFormat();
sf.Alignment = HorizontalAlignment.Center;
sf.LineAlignment = VerticalAlignment.Center;

// 各用紙サイズにつき1つのページを作成します。
bool firstPage = true;
foreach (var fi in typeof(PaperKind).GetFields(System.Reflection.BindingFlags.Static
     | System.Reflection.BindingFlags.Public))
{
    // Silverlight/Phone doesn't have Enum.GetValues
    PaperKind pk = (PaperKind)fi.GetValue(null);

    // カスタムサイズはスキップします。
    if (pk == PaperKind.Custom) continue;

    // 最初のページ以降のすべてのページに新しいページを追加します。
    if (!firstPage) pdf.NewPage();
    firstPage = false;

    // 用紙の種類および方向を設定します。
    pdf.PaperKind = pk;
    pdf.Landscape = !pdf.Landscape;

    // コンテンツをページに描画します。

    string text = string.Format("PaperKind: [{0}];\r\nLandscape: [{1}];\r\nFont: [Tahoma 18pt]",
        pdf.PaperKind, pdf.Landscape);
    pdf.DrawString(text, font, Colors.Black, rect, sf);
    pdf.DrawRectangle(Colors.Black, rect);
}

ドキュメントに追加された最終ページ以外にも書き込むことができます。C1PdfDocument.CurrentPage プロパティを使用して書き込み対象のページを選択し、通常の描画コマンドを通常どおりに使用します。これは、ドキュメントのレンダリングを完了した後で、コンテンツをページに追加する場合に役立ちます。たとえば、次のコードは、現在のページ番号と総ページ数を含むフッター("Page n of m")をドキュメント内の各ページに追加します。

Visual Basic
コードのコピー
Dim font As New Font("Tahoma", 7, PdfFontStyle.Bold)
Dim sf As New StringFormat()
sf.Alignment = HorizontalAlignment.Center
For page As Integer = 0 To pdf.Pages.Count - 1
    ' ページを選択します。
    pdf.CurrentPage = page
    ' フッターをレンダリングするための四角形を構築します。
    Dim rect As Rect = pdf.PageRectangle
    rect.Y = rect.Bottom - 36
    ' フッターを書き込みます。
    Dim text As String = String.Format("Page {0} of {1}", page + 1, pdf.Pages.Count)
    pdf.DrawString(text, font, Colors.Gray, rect, sf)
Next

C#
コードのコピー
Font font = new Font("Tahoma", 7, PdfFontStyle.Bold);

StringFormat sf = new StringFormat();
sf.Alignment = HorizontalAlignment.Center;
for (int page = 0; page < pdf.Pages.Count; page++)
{
    // ページを選択します。
    pdf.CurrentPage = page;
    // フッターをレンダリングするための四角形を構築します。

    Rect rect = pdf.PageRectangle;

    rect.Y = rect.Bottom - 36;
    // フッターを書き込みます。
    string text = string.Format("Page {0} of {1}", page + 1, pdf.Pages.Count);
    pdf.DrawString(text, font, Colors.Gray, rect, sf);
}

コードは、C1PdfDocumentBase.Pages プロパティを使用してページ数を取得します。Pages は、ArrayList クラスに基づくコレクションであり、ページのカウントと列挙を行うメソッドや、特定の位置にあるページの追加と削除を行うメソッドを持ちます。Pages コレクションを使用すると、ページをドキュメント内の特定の位置から削除して任意の位置に挿入できます。